package com.example.unlock2

import android.annotation.SuppressLint
import android.os.HandlerThread
import android.view.View
import android.widget.ImageView
import java.util.logging.Handler

class UnlockPresenter(
    val mView:IUnlockView,
    var mDotViewList: List<ImageView>,
    val mViewModelsList: List<ImageViewModel>,
    val mLineTagsList: List<Int>
) {
    private lateinit var user:User
    //记录上一次被点亮的视图
    private var lastSelectedDot:ImageView? = null
    //记录所有被点的视图
    private var selectedModel = arrayListOf<ImageView>()
    //记录密码
    private var passwordBuilder = StringBuilder()
    //记录第一次密码
    private var firstPassword = ""
    fun loadUserInfo(){
        user = User("huxiuli","123",true)
        if(user.isLogin){
            mView.showAlertText("请绘制密码")
        }else{
            mView.showAlertText("请设置密码")
        }
    }
    @SuppressLint("SuspiciousIndentation")
    fun userTouch(x:Float, y:Float,) {
     val dot:ImageView = isInView(x,y) ?:return
          if(lastSelectedDot == null){
              HighLight(dot)
          }else{
              //判断路线是否有
            val lineView:ImageView =  hasLine(dot) ?:return
              //点亮线
              HighLight(dot)
              highlightLine(lineView)
          }
      }
    fun endTouch(){
        if(user.isLogin){
            if(user.password == passwordBuilder.toString()){
                mView.showAlertText("解锁成功")
                changeBlack()
                mView.switchActivity()

            }else{
                mView.showAlertText("解锁失败，请重新绘制")
                changeColor()
                clear()
                android.os.Handler().postDelayed({
                    changeBlack()
                },500)
            } 

        }else{
            if(firstPassword.isEmpty()){
                firstPassword = passwordBuilder.toString()
                mView.showAlertText("请确认密码")
                lastSelectedDot = null
                passwordBuilder.clear()
            }else{
                if(firstPassword == passwordBuilder.toString()){
                    mView.showAlertText("设置密码成功")
                    lastSelectedDot = null
                    clear()
                }else{
                    mView.showAlertText("密码不一致，请重新绘制")
                    changeColor()
                    clear()
                }
            }
        }
        hideView()
    }
    private fun isInView(x:Float,y:Float):ImageView?{
       mDotViewList.forEach {
           if((x>=it.x&&x<=it.x+it.width)&&(y>=it.y&&y<=it.y+it.height)){
               return it
           }
       }
     return null
   }
    private fun HighLight(dotView:ImageView){
        dotView.visibility = View.VISIBLE
        passwordBuilder.append(dotView.tag.toString())
        lastSelectedDot = dotView
        selectedModel.add(dotView)
    }
    private fun highlightLine(lineView:ImageView){
        lineView.visibility = View.VISIBLE
        selectedModel.add(lineView)
    }
    private fun hasLine(view:ImageView):ImageView?{
        val last = (lastSelectedDot!!.tag as String).toInt()
        val current = (view.tag as String).toInt()
        val lineTag:Int = if(last<current){
            last*10+current
        }else{
            current*10+last
        }
        //判断lineTagList中是否有这个值
        if(mLineTagsList.contains(lineTag)){
            return findLineTag(lineTag)
        }
        return null
    }
    private fun findLineTag(LineTag:Int):ImageView?{
        mViewModelsList.forEach {
            if(it.view.tag == "$LineTag"){
                return it.view
            }
        }
        return null
    }
    private fun clear(){
        firstPassword = ""
        lastSelectedDot = null
        passwordBuilder.clear()
    }
    private fun hideView(){
        android.os.Handler().postDelayed({
            selectedModel.forEach { it.visibility = View.INVISIBLE }
            selectedModel.clear()
        },500)
    }
    private fun changeColor(){
        mViewModelsList.forEach {
           it.changeImage(it.errorResId)
        }
    }
    private fun changeBlack(){
        mViewModelsList.forEach {
            it.changeImage(it.normalResId)
        }
    }
}
